package com.sdhs.paas.codegenerate.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import com.sdhs.paas.codegenerate.utils.FreeMarkerTemplateUtils;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class GenerateVueService {

	/**
	 * api.js生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateApiJs(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		String moduleName = String.valueOf(attrs.get("moduleName")).toLowerCase();
		map.put("apiPath", attrs.get("apiPath"));
		map.put("gatewayPrefix", attrs.get("gatewayPrefix"));
		map.put("author", attrs.get("author"));
		map.put("moduleName", attrs.get("moduleName"));
		map.put("date", attrs.get("date"));
		String realPath = String.valueOf(attrs.get("apiPath"));
		final String templateName = "VueApiJs.ftl";
		final String suffix = ".js";
		final String path = realPath + moduleName + suffix;
		generateFileByTemplate(templateName, map, path);
		return realPath + "." + attrs.get("className");
	}
	
	/**
	 * vue展示页面生成器
	 * 
	 * @param attrs
	 *            需要解析的参数
	 * @return 返回实体的类路径
	 */
	public static String generateViewsVue(Map<String, Object> attrs) {
		Map<String,Object> map = new HashMap<String,Object>();
		String moduleName = String.valueOf(attrs.get("moduleName")).toLowerCase();
		String projectName = String.valueOf(attrs.get("projectName"));
		map.put("viewsPath", attrs.get("viewsPath"));
		map.put("author", attrs.get("author"));
		map.put("moduleName", attrs.get("moduleName"));
		map.put("listQuery", attrs.get("listQuery"));
		map.put("attrs", attrs.get("nativecolumns"));
		map.put("prefix", "/api/"+projectName+"/"+moduleName);
		map.put("date", attrs.get("date"));
		String realPath = String.valueOf(attrs.get("viewsPath"));
		final String templateName = "VueViews.ftl";
		final String suffix = ".vue";
		final String path = realPath + moduleName+"Index" + suffix;
		generateFileByTemplate(templateName, map, path);
		return "生成vue页面代码生成功";
	}
	
	/**
	 * 代码生成器
	 * 
	 * @param templateName
	 *            使用的模板名称
	 * @param attrs
	 *            赋值的参数
	 * @param path
	 *            生成的路径
	 */
	private static void generateFileByTemplate(String templateName, Map<String, Object> attrs, String path) {
		File file = new File(path);
		// 检测是否存在目录
        if (!file.getParentFile().exists()) {
        	file.getParentFile().mkdirs();// 新建文件夹
        }
		Template template = null;
		try {
			template = FreeMarkerTemplateUtils.getTemplate(templateName);
			FileOutputStream fos = new FileOutputStream(file);
			Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
			template.process(attrs, out);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
}
